const app = getApp()
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    buyCount: 0,
    sellCount: 0,
    purchaseCount: 0,
    latestDeal: null, // 最新交易
    latestPost: null, // 最近发布

    //等待框
    waitHidden: false,
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight, //导航栏高度
    //页面尺寸数据
    windowHeight: 0,
    screenHeight: 0,
    userId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      userId: wx.getStorageSync('logindata').data.id,
      username: wx.getStorageSync('userName')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.setData({
      waitHidden: false
    })

    that.getCounts()
    that.getMyLastestDeal()
    that.getMyRecentPost()
    setTimeout(res => {
      that.setData({
        waitHidden: true
      })
    }, 500)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  tapPurchaseList: function () {
    wx.navigateTo({
      url: '/pages/marketCenter2/requireGoods/requireGoods',
    })
  },
  tapSoldList: function () {
    wx.navigateTo({
      url: '/pages/marketCenter2/boughtOrSoldGoods/boughtOrSoldGoods?index=0',
    })
  },
  tapBoughtList: function () {
    wx.navigateTo({
      url: '/pages/marketCenter2/boughtOrSoldGoods/boughtOrSoldGoods?index=1',
    })
  },


  goBack: function (e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  getCounts: function () {
    this.getMyBoughtCount()
    this.getMyPurchaseCount()
    this.getMyPostsCount()
  },

  getMyPostsCount: function () {
    var that = this
    wx.request({
      url: api.getMyPostsCount + '?user_id=' + that.data.userId,
      method: "GET",
      success(res) {
        that.setData({
          sellCount: res.data.data[0].count
        })
      }
    })
  },

  getMyPurchaseCount: function () {
    var that = this
    wx.request({
      url: api.getMyPurchaseCount + '?user_id=' + that.data.userId,
      method: "GET",
      success(res) {
        that.setData({
          purchaseCount: res.data.data[0].count
        })
      }
    })
  },

  getMyBoughtCount: function () {
    var that = this
    wx.request({
      url: api.getMyBuyCount + '?user_id=' + that.data.userId,
      method: "GET",
      success(res) {
        that.setData({
          buyCount: res.data.data[0].count
        })
      }
    })
  },

  // 最近交易
  getMyLastestDeal: function () {
    var that = this
    wx.request({
      url: api.getMyRecentOrder + '?user_id=' + that.data.userId,
      method: "GET",
      success(res) {
        if (res.data.data.length != 0) {
          that.setData({
            latestDeal: {
              title: res.data.data[0].title,
              price: res.data.data[0].price,
              status: res.data.data[0].status,
              bottomPrice: res.data.data[0].bottomPrice,
              peakPrice: res.data.data[0].peakPrice,
              imgUrl: res.data.data[0].imgUrl,
              isRequire: res.data.data[0].is_demand == "1" ? true : false
            }
          })
        } else {
          that.setData({
            latestDeal: null
          })
        }
      }
    })
  },

  // 最新发布
  getMyRecentPost: function () {
    var that = this
    wx.request({
      url: api.getRecentPost + '?user_id=' + that.data.userId,
      method: "GET",
      success(res) {
        if (res.data.data.length != 0) {
          if (res.data.data[0].isDemand == "1") { // 求购
            that.setData({
              latestPost: {
                title: res.data.data[0].title,
                bottomPrice: res.data.data[0].bottomPrice,
                peakPrice: res.data.data[0].peakPrice,
                imgUrl: res.data.data[0].imgUrl,
                status: res.data.data[0].status,
                isRequire: true
              }
            })
          } else if (res.data.data[0].isDemand == "0") { // 闲置
            that.setData({
              latestPost: {
                title: res.data.data[0].title,
                price: res.data.data[0].bottomPrice,
                imgUrl: res.data.data[0].imgUrl,
                status: res.data.data[0].status,
                isRequire: false
              }
            })
          }
        } else {
          that.setData({
            latestPost: null
          })
        }
      }
    })
  }
})